package com.hsylient.javase.chapter10;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;

@Slf4j
public class StrUtilTest {


    @Test
    public void testBrief() {
        String source = "1CdKGfC5HjfGcnomepSwNc5jjSaQbY19dF";
        String target = StrUtil.brief(source, 10);
        log.info("[字符串简化] 原字符串[{}] 简化后的字符串[{}]", source, target);
    }

    @Test
    public void testSubString() {
        String origin = "hsylient.png";
        String destination = StrUtil.subAfter(origin, StrUtil.DOT, Boolean.TRUE);
        log.info("[字符串截取] 原字符串[{}] 截取后的字符串[{}]", origin, destination);

    }

    @Test
    public void replaceString() {
//        String oldString = "12345678910"; // 11位
        String oldString = "123456789"; // 9位
//        String oldString = ""; // 空串
//        String oldString = null; // NPE
//        String oldString = "xx";

        String newString2 = StrUtil.replace(oldString, 8, oldString.length(), StrUtil.DOT);
        log.info("[字符串替换] {}被替换后得到{}", oldString, newString2);

        if (oldString.length() > 9) {
            String newString = StrUtil.replace(oldString, 8, oldString.length(), StrUtil.DOT);
            log.info("[字符串替换] {}被替换后得到{}", oldString, newString);
        }
    }

    /**
     * 将一个集合中的数据通过 - 连接
     */
    @Test
    public void testListJoin() {
        List<String> origin = List.of("a", "b", "c", "D");
        String destination = StrUtil.join("-", origin);
        log.info("[字符串拼接] 原字符串集合[{}] 拼接后的字符串[{}]", origin, destination);
    }

    @Test
    public void testSplit() {
        String origin = "{\"link\":\"/cn/help/in-celebration-of-the-shanghai-upgrade-okx-earn-will-launch-beth-flash-deals\",\"publishDate\":1681274997000,\"shareLink\":\"https://www.okx.com/cn/help/in-celebration-of-the-shanghai-upgrade-okx-earn-will-launch-beth-flash-deals\",\"shareTitle\":\"欧易为庆祝以太坊上海升级，开启BETH质押赚币活动的公告\",\"sort\":1739,\"startTime\":1681274997000,\"text\":\"欧易为庆祝以太坊上海升级，开启BETH质押赚币活动的公告\",\"title\":\"欧易为庆祝以太坊上海升级，开启BETH质押赚币活动的公告\"}\n" +
                "{\"link\":\"/cn/help/okx-earn-celebrates-ethereum-shanghai-upgrade-with-exclusive-airdrop\",\"publishDate\":1680771600000,\"shareLink\":\"https://www.okx.com/cn/help/okx-earn-celebrates-ethereum-shanghai-upgrade-with-exclusive-airdrop\",\"shareTitle\":\"欧易赚币关于庆祝以太坊上海升级系列活动的公告：增加BETH持仓，享 ETH2.0 质押专属空投\",\"sort\":1732,\"startTime\":1680771600000,\"text\":\"欧易赚币关于庆祝以太坊上海升级系列活动的公告：增加BETH持仓，享 ETH2.0 质押专属空投\",\"title\":\"欧易赚币关于庆祝以太坊上海升级系列活动的公告：增加BETH持仓，享 ETH2.0 质押专属空投\"}\n" +
                "{\"link\":\"/cn/help/okx-earn-will-launch-flr-earn-product\",\"publishDate\":1680164534000,\"shareLink\":\"https://www.okx.com/cn/help/okx-earn-will-launch-flr-earn-product\",\"shareTitle\":\"欧易赚币关于支持FLR锁仓赚币的公告\",\"sort\":1711,\"startTime\":1680164534000,\"text\":\"欧易赚币关于支持FLR锁仓赚币的公告\",\"title\":\"欧易赚币关于支持FLR锁仓赚币的公告\"}";

        String[] destination = origin.split("\n");
        log.info("[字符串数组] {}", Arrays.toString(destination));
    }


    @Test
    public void testFormat() {
        String destination = StrUtil.format("我真的是{},我真的是{},{}", "嘎嘎帅", "嘎嘎聪明");
        log.info("[字符串格式化] {}", destination);
    }
}
